﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XOROperation
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    class Programp
    {
        public static StringBuilder result = new StringBuilder();

        public static void AddZero(string beiChuShu, int? yuShu, int? chuShu)
        {
            //Validation
            if (beiChuShu == null) return;
            if (yuShu == null) return;
            if (chuShu == null) return;

            //Preparation
            String stringChuShu = chuShu.ToString();
            int chuShuLength = stringChuShu.ToString().Length;

            string stringYuShu;
            int yuShuLength;
            if (yuShu == 0)
            {
                yuShuLength = 0;

            }
            else
            {
                stringYuShu = yuShu.ToString();
                yuShuLength = stringYuShu.Length;
            }
            int beiChuShuLength = beiChuShu.Length;
            //这里是要干什么呢
            if (int.Parse(beiChuShu) < int.Parse(stringChuShu))
            {
                return;
            }
            string temp = beiChuShu.Substring(0, chuShuLength);
            //如果前面的
            if (int.Parse(temp) < int.Parse(stringChuShu))
            {
                //补上chuShuLength-yuShuLength 个零
                for (int i = 0; i < chuShuLength - yuShuLength; i++)
                {
                    result.Append('0');
                }
            }
            else
            {
                //补上chuShuLength-yuShuLength-1个零
                for (int i = 0; i < chuShuLength - yuShuLength - 1; i++)
                {
                    result.Append('0');
                }
            }
        }

        public static string GetPart(string part)
        {
            if (part == null) return null;
            if (part == "") return null;
            bool isBiggerThanMax = false;

            //如果截取的字符串比maxvalue大 
            try
            {
                UInt64.Parse(part);
            }
            catch (Exception e)
            {
                isBiggerThanMax = true;
            }
            //比最大之还要大，那么向前移动一个位置
            if (isBiggerThanMax)
            {
                //截取的字符串的长度减1                  
                part = part.Substring(0, part.Length - 1);
            }
            return part;
        }

        public static StringBuilder SpecialDivide(String beiChuShu, int? chuShu)
        {
            //Validation
            if (beiChuShu == null)
                return null;
            if (chuShu == null)
                return null;
            if (string.Compare(beiChuShu, chuShu.ToString()) < 0)
                return null;

            //计算机所能表示的最大数
            String maxValue = UInt64.MaxValue.ToString();
            //最大数的长度
            int maxValueLength = maxValue.Length;
            //字符串的长度
            int beiChuShuLength = beiChuShu.Length;
            //开始截取字符串的起始位置
            int startIndex = 0;
            //被除整数的长度
            int chuShuLength = chuShu.ToString().Length;
            //被除证书的字符创表示
            string chuShuString = chuShu.ToString();
            String part = GetPart(beiChuShu.Substring(0, maxValueLength));
            startIndex = startIndex + part.Length;
            int yuShuLength = 0;
            UInt64 yuShu = 0;

            while (true)
            {
                UInt64 a = UInt64.Parse(part);
                UInt64 resultPart = a / (uint)chuShu;
                if (resultPart <= 0)
                {
                    break;
                }
                result.Append(resultPart);
                yuShu = a % (uint)chuShu;
                //如果余数不为0
                if (yuShu != 0)
                {
                    //余数的长度
                    yuShuLength = yuShu.ToString().Length;
                    //字符串够长  这个地方不需要+1
                    if (startIndex + maxValueLength - yuShuLength <= beiChuShuLength - 1)
                    {
                        //截取的下一个除数的字符串
                        part = GetPart(yuShu.ToString() + beiChuShu.Substring(startIndex, maxValueLength - yuShuLength));

                        //startIndex向前移动blength位
                        startIndex = startIndex + part.Length - yuShuLength;
                        string partTemp = part.Substring(0, chuShuLength);
                        AddZero(partTemp, (int)yuShu, chuShu);
                    }
                    else
                    {
                        //全部取出来，重新构建一个part
                        part = GetPart(yuShu.ToString() + beiChuShu.Substring(startIndex, beiChuShuLength - startIndex));
                        if (part == "" || part == null)
                            break;
                        startIndex = beiChuShuLength;
                        string partTemp = part.Substring(0, chuShuLength);
                        AddZero(partTemp, (int)yuShu, chuShu);
                    }
                }
                //如果余数为零
                else
                {
                    yuShu = 0;
                    //如果接下来的是0 补零                   
                    while (startIndex <= beiChuShuLength - 1 && (beiChuShu[startIndex] == '0'))
                    {
                        result.Append(beiChuShu[startIndex]);
                        startIndex++;
                    }

                    //碰到数字
                    if ((startIndex + maxValueLength <= beiChuShuLength - 1) && !(beiChuShu[startIndex] == '0'))
                    {
                        //这里也需要补零
                        part = GetPart(beiChuShu.Substring(startIndex, maxValueLength));
                        startIndex = startIndex + part.Length - yuShuLength;
                        string partTemp = part.Substring(startIndex, chuShuLength);
                        AddZero(partTemp, (int)yuShu, chuShu);
                    }
                    else
                    {
                        part = GetPart(beiChuShu.Substring(startIndex, beiChuShuLength - startIndex));
                        if (part == "" || part == null)
                            break;
                        string partTemp = part.Substring(0, chuShuLength);
                        startIndex = beiChuShuLength;
                        AddZero(partTemp, (int)yuShu, chuShu);
                    }
                }
            }
            return result;
        }
        static void Main(string[] args)
        {
            //String a = "85221122121212121522588222115812233455228525";
            //136353795393939394436141155385299573528365
            string a = "10000000000000000000000000000000010000000000";
            
            int b = 100;
            Console.WriteLine(SpecialDivide(a, b));
            Console.WriteLine(SpecialDivide("2222", 3));
            Console.Read();
        }
    }
}
